
/**
 * 封装http请求
 * */
const ip = {
	default: 'http://192.168.1.156:8889'
}

function getIp(pary) {
	return ip[pary || 'default']
}
function getWs(pary) {
	return ip[pary || 'default'].replace('https://', 'wss://').replace('http://', 'ws://') + 'websocket/'
}
/**
 * @param {Object} pary
 * @param {Object} url
 * @param {Object} method
 * @param {Object} header
 * @param {Object} param
 * @param { true - 开启token 、 false - 关闭token } hasToken
 * @param { true - 开启加载 、 false - 关闭加载 } hasLoading
 * @param { true - 开启提示 、 false - 关闭提示 } hasToast
 */
function httpRequest(pary, url, method, param, header, hasToken, hasLoading, hasToast) {
	let obj = {}
	if (!hasToken) {
		obj = {
			'Content-Type': 'application/json;charset=UTF-8',
			'token': store.state.base.token
		}
	} else {
		obj = {
			'Content-Type': 'application/json;charset=UTF-8'
		}
	}
	return new Promise((resolve, reject) => {
		if (hasLoading) {
			uni.showLoading({
				title: '请求中...'
			})
		}
		uni.request({
				url: ip[pary || 'default'] + url,
				method: method,
				header: header | method == 'GET' ? obj : obj,
				data: param,
				dataType: 'json'
			})
			.then(res => {
				console.log(res)
				if (hasLoading) {
					uni.hideLoading()
				}
				let data = res[1]
				return resolve(data)
			})
			.catch(ret => {
				// this.$u.toast('网络服务器繁忙, 请稍后')
				return reject(ret)
			})
	})
}
export {
	getIp,
	getWs,
	httpRequest
}